package org.spring.springboot.app.user.sysuser.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.Menu;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.user.sysuser.domain.po.SysUserPO;
import org.spring.springboot.app.user.sysuser.domain.vo.TokenResVO;
import org.spring.springboot.app.user.sysuser.domain.vo.UserTokenReqVO;
import org.spring.springboot.app.user.sysuser.service.ISysUserService;
import org.spring.springboot.util.ApiIndex;
import org.spring.springboot.util.RedisUtils;
import org.spring.springboot.util.IdGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(description = "安全相关接口",tags = ApiIndex.SecureController)
@RestController
public class SecureController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisUtils redisUtils;


    @ApiOperation(value = "取得token",notes = "670b14728ad9902aecba32e22fa4f6bd")
    @RequestMapping(value = "/api/token", method = RequestMethod.POST)
    @ResponseBody
    public R<TokenResVO> token(
            HttpServletRequest request,
            @RequestBody UserTokenReqVO user) {
        R result = new R();
        if (StringUtils.isBlank(user.getLoginId()) || StringUtils.isBlank(user.getPassword())) {
            result.setType(Type.ParamValidateFail);
            return result;
        }
        SysUserPO sysUserPO = sysUserService.findByLoginId(user.getLoginId());
        if (sysUserPO == null) {
            result.setType(Type.NotFoundError);
            result.setMessage("账号不存在");
            return result;
        }
        if ("1".equals(sysUserPO.getDelFlag())) {
            result.setType(Type.DataDelError);
            result.setMessage("账号已被删除");
            return result;
        }
        if (!"1".equals(sysUserPO.getLoginFlag())) {
            result.setType(Type.DataDelError);
            result.setMessage("账号已被禁用");
            return result;
        }
        if (!sysUserPO.getPassword().equalsIgnoreCase(user.getPassword())) {
            result.setType(Type.PowerValidateFail);
            result.setMessage("你的密码已被修改,请重新登录");
            return result;
        }
        String token = IdGen.uuid();
        User userSession = new User();
        BeanUtils.copyProperties(sysUserPO, userSession);
        List<Menu> menus = sysUserService.selectUserMenus(sysUserPO.getId());
        userSession.setMenus(menus);
        TokenResVO tokenResVO = new TokenResVO();
        tokenResVO.setToken(token);
        result.setData(tokenResVO);
        redisUtils.set(token, userSession, 60 * 60, TimeUnit.SECONDS);
        return result;
    }

}
